﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Master;
using System.ServiceModel;

namespace BranchOfSansar
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
            txnUserName.Text = Properties.Settings.Default.LastUserName;
            if (!string.IsNullOrEmpty(Properties.Settings.Default.LastUserName))
            {
                txtPassword.Focus();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                using (MasterClient obj = new MasterClient(new Token("")))
                {
                    var res = obj.Login(txnUserName.Text, txtPassword.Text);
                    if (res.Code == 0)
                    {
                        Globals.Token = new Token(res.Message);
                        Globals.UserId = (int)res.RetData;
                        Globals.SystemDate = DateTime.Now;
                        Globals.UserName = txnUserName.Text;
                        Properties.Settings.Default.LastUserName = Globals.UserName;
                        Properties.Settings.Default.Save();
                        DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(res.Message, "Алдаа", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txnUserName.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                Globals.Warr("Сервер унтарсан байна:"+ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Properties.Settings.Default.LastUserName))
            {
                txtPassword.Focus();
            }
        }

     
    }
}
